﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDT.Lib.Common;
using System.Linq.Dynamic;
using SDT.Lib.Helpers;
using SDT.Model;
namespace SDT.Admin.Controllers
{
    [RoleHelper(Roles = RoleSystem.Dictionary)]
    public class PositionController : Controller
    {
        //
        // GET: /Position/

        public ActionResult Index()
        {
            ChoVietDataContext db = new ChoVietDataContext();
            string order = UserState.getUserStateFromRequest("position.order", "order", "ID").ToString();
            string orderDir = UserState.getUserStateFromRequest("position.orderDir", "orderDir", "desc").ToString();

            var l = db.Positions.OrderBy(string.Format("{0} {1}", order, orderDir)).ToList();

            ViewData["orderDir"] = orderDir;
            ViewData["order"] = order;

            return View(l);
        }

        //
        // GET: /Position/Details/5

        public ActionResult Details(int id)
        {
            ChoVietDataContext db = new ChoVietDataContext();
            var p = db.Positions.Single(e => e.ID == id);
            return View(p);
        }

        //
        // GET: /Position/Create

        public ActionResult Create()
        {
            var p = new Position();
            return View(p);
        } 

        //
        // POST: /Position/Create

        [HttpPost]
        public ActionResult Create(Position p)
        {
            try
            {
                // TODO: Add insert logic here
                ChoVietDataContext db = new ChoVietDataContext();
                UpdateModel(p);
                p.ShareYN = Request["chkShare"].ToLower() != "false" ? true : false;
                db.Positions.InsertOnSubmit(p);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Position/Edit/5
 
        public ActionResult Edit(int id)
        {
            ChoVietDataContext db = new ChoVietDataContext();
            var p = db.Positions.Single(e => e.ID == id);
            return View(p);
        }

        //
        // POST: /Position/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Position p)
        {
            try
            {
                // TODO: Add update logic here
                ChoVietDataContext db = new ChoVietDataContext();
                p = db.Positions.Single(e => e.ID == id);
                UpdateModel(p);
                p.ShareYN = Request["chkShare"].ToLower() != "false" ? true : false;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Position/Delete/5
 
        public ActionResult Delete(int id)
        {

            ChoVietDataContext db = new ChoVietDataContext();
            var p = db.Positions.Single(e => e.ID == id);
            return View(p);
        }

        //
        // POST: /Position/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                ChoVietDataContext db = new ChoVietDataContext();
                var p = db.Positions.Single(e => e.ID == id);
                db.StaffPositionMaps.DeleteAllOnSubmit(from d in db.StaffPositionMaps where d.PositionID==id select d);
                db.Positions.DeleteOnSubmit(p);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
